package com.sunop.comment.controller;

import com.sunop.panticommon.entity.Result;
import com.sunop.comment.model.Comment;
import com.sunop.comment.model.CommentPageQueryDTO;
import com.sunop.comment.model.CommentReplyDTO;
import com.sunop.comment.model.CommentSendDTO;
import com.sunop.comment.service.CommentService;
import com.sunop.panticommon.entity.PageResult;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @since 2025-03-25
 */
@RestController
@RequestMapping("/comment")
@AllArgsConstructor
public class CommentController {

    private final CommentService commentService;

    @PostMapping("/send")
    public Result sendComment(@RequestBody CommentSendDTO commentSendDTO) {
        return commentService.sendComment(commentSendDTO);
    }

    @GetMapping("/list")
    public Result<PageResult> listPageQuery(@RequestBody CommentPageQueryDTO commentPageQueryDTO) {
        try {
            PageResult pageResult = commentService.pageQuery(commentPageQueryDTO);
            return Result.success(pageResult);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/{id}")
    public Result<Comment> getCommentInfo(@PathVariable Integer id) {
        try {
            Comment comment = commentService.getById(id);
            return Result.success(comment);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestParam("id") Integer id) {
        return commentService.delById(id);
    }

    @PostMapping("/{commentId}/like")
    public Result like(@PathVariable Integer commentId) {
        return commentService.commentLike(commentId);
    }

    @PostMapping("/{commentId}/unlike")
    public Result unlike(@PathVariable Integer commentId) {
        return commentService.commentUnlike(commentId);
    }

    @PostMapping("/sendReply")
    public Result sendReply(@RequestBody CommentReplyDTO commentSendDTO) {
        return commentService.sendReply(commentSendDTO);
    }

    @GetMapping("/listReply/{id}")
    public Result<PageResult> listReply(@PathVariable Integer id, @RequestBody CommentPageQueryDTO commentPageQueryDTO) {
        try {
            PageResult pageResult = commentService.replyPageQuery(commentPageQueryDTO, id);
            return Result.success(pageResult);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @DeleteMapping("/deleteReply")
    public Result deleteReply(@RequestParam("id") Integer id) {
        return commentService.delReplyById(id);
    }

    // 收藏讨论帖
    @PostMapping("/{commentId}/collect")
    public Result collect(@PathVariable Integer commentId) {
        return commentService.collectComment(commentId);
    }

    // 取消收藏
    @DeleteMapping("/{commentId}/collect")
    public Result uncollect(@PathVariable Integer commentId) {
        return commentService.uncollectComment(commentId);
    }

    // 查看收藏列表（分页 + 排序）false 正序，true 倒序
    @GetMapping("/collect")
    public Result getCollectedComments(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "false") boolean reverseOrder) {
        return commentService.getCollectedComments(page, size, reverseOrder);
    }

    // 获取收藏总数
    @GetMapping("/collect/count")
    public Result getCollectedCount() {
        long count = commentService.getCollectedCount();
        return Result.success(count, "获取收藏总数成功");
    }

    // 检查是否已收藏
    @GetMapping("/{commentId}/collect/status")
    public Result isCollected(@PathVariable Integer commentId) {
        boolean isCollected = commentService.isCommentCollected(commentId);
        return Result.success(isCollected, "检查收藏状态成功");
    }
}
